package org.study.plus.boot.service;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.study.plus.boot.mapper.DatabaseTableMapper;
import org.study.plus.boot.vo.ColumnInfoVo;
import org.study.plus.boot.vo.TableInfoVo;

import java.util.List;
import java.util.Map;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年03月31日
 */
@Slf4j
@Service
@AllArgsConstructor
public class DatabaseTableServiceImpl implements DatabaseTableService {
    private DatabaseTableMapper databaseTableMapper;

    @Override
    public List<TableInfoVo> getTables(String schemaName, String tablePrefix) {
        List<TableInfoVo> result = Lists.newArrayList();

        List<Map<String, String>> tables = this.databaseTableMapper.getTables(schemaName, tablePrefix);
        if (CollectionUtils.isNotEmpty(tables)) {
            tables.forEach(map -> {
                String name = map.get("Name");
                String comment = map.get("Comment");
                result.add(new TableInfoVo(name, comment));
            });
        }

        return result;
    }

    @Override
    public List<ColumnInfoVo> getColumns(String schemaName, String tableName) {
        List<ColumnInfoVo> result = Lists.newArrayList();

        List<Map<String, String>> columns = this.databaseTableMapper.getColumns(schemaName, tableName);
        if (CollectionUtils.isNotEmpty(columns)) {
            columns.forEach(map -> {
                String name = map.get("Field");
                String type = map.get("Type");
                String comment = map.get("Comment");
                result.add(new ColumnInfoVo(name, type, comment));
            });
        }
        return result;
    }
}
